package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.model.TaskReceiver;
import ge.telasi.tasks.ui.UIUtils;
import java.util.List;
import javax.swing.JPanel;

/**
 * @author dimitri
 */
public class TaskReceiverDisplayPanel extends JPanel {

    public TaskReceiverDisplayPanel() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        list = new ge.telasi.tasks.ui.task.TaskReceiverList();
        btnChange = new javax.swing.JButton();

        jScrollPane1.setViewportView(list);

        btnChange.setText("შეცვლა...");
        btnChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(btnChange)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnChange))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeActionPerformed
        TaskReceiverEditDialog dialog = new TaskReceiverEditDialog(this);
        dialog.setTask(task, copy);
        UIUtils.openDialog(dialog);
        if (dialog.isApproved()) {
            Task newTask = dialog.getTask();
            list.setReceivers(copy ? newTask.getCopies() : newTask.getAssignees());
            task.setReceivers(newTask.getReceivers());
        }
    }//GEN-LAST:event_btnChangeActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChange;
    private javax.swing.JScrollPane jScrollPane1;
    private ge.telasi.tasks.ui.task.TaskReceiverList list;
    // End of variables declaration//GEN-END:variables

    private Task task;
    private boolean copy;

    public Task getTask() {
        return task;
    }

    public void setTask(Task task, boolean copy) {
        this.task = task;
        this.copy = copy;
        list.setReceivers(copy ? task.getCopies() : task.getAssignees());
    }
    
    public List<TaskReceiver> getReceiver() {
        return list.getReceivers();
    }

    public boolean isCopy() {
        return copy;
    }
    
}
